import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler#标准化
from sklearn.linear_model import LinearRegression,SGDRegressor#线性回归模型，正规方程法和SGD
from sklearn.metrics import mean_squared_error#均方误差损失函数

#1.读取数据集
dataset = pd.read_csv('../data/advertising.csv')

dataset.dropna(inplace=True)
dataset.drop(columns=dataset.columns[0],axis=1,inplace=True)
dataset.info()
print(dataset.describe())

#2.划分训练集和测试集
X = dataset.drop(columns='Sales')
Y = dataset['Sales']
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3, random_state=42)
print(X_train.shape, X_test.shape, Y_train.shape, Y_test.shape)

#3.特征工程
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

#4.创建模型并训练
#4.1正规方程法
model_lr = LinearRegression()
model_lr.fit(X_train, Y_train)

print("LR Coefficients:", model_lr.coef_)
print("LR Intercept:", model_lr.intercept_)

#4.1 SGD
model_sgd = SGDRegressor()
model_sgd.fit(X_train, Y_train)

print("SGD Coefficients:", model_sgd.coef_)
print("SGD Intercept:", model_sgd.intercept_)
#5.预测
Y_pred1 = model_lr.predict(X_test)
Y_pred2 = model_sgd.predict(X_test)

#6.使用均方误差评价模型
print("LR Mean Squared Error:", mean_squared_error(Y_test, Y_pred1))
print("SGD Mean Squared Error:", mean_squared_error(Y_test, Y_pred2))


